﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using CellarDoor.Gui.Elements;
using CellarDoor.Gui.Animation;

namespace CellarDoor.Gui.Demo.Xbox
{
    public class Dashboard : UiFrame
    {
        KeyboardState oldKey;
        KeyboardState newKey;

        DashboardMenu menu;

        public Dashboard(UiManager manager)
            : base(manager)
        {
            Initialize();
        }

        private void Initialize()
        {
            this.Width = Manager.NativeViewport.Width;
            this.Height = Manager.NativeViewport.Height;
            this.ElementName = "BackgroundDark";

            menu = new DashboardMenu(this);
            menu.Width = Manager.NativeViewport.Width;
            menu.Height = Manager.NativeViewport.Height;

            MockPanels("Xbox LIVE");
            MockPanels("My Xbox");
            MockPanels("Friends");
            MockPanels("Video Marketplace");
            MockPanels("Game Marketplace");
            MockPanels("Welcome");

            menu.SelectedItem.Return();
        }

        private void MockPanels(string text)
        {
            var item = this.menu.AddEntry(text);
            for (int i = 0; i < 10; i++)
            {
                item.AddItem().Text = text + i.ToString();
            }
        }

        public override void Update(GameTime gameTime)
        {
            oldKey = newKey;
            newKey = Keyboard.GetState();

            if (newKey.IsKeyDown(Keys.Left) && oldKey.IsKeyUp(Keys.Left))
            {
                menu.SelectedItem.ShiftPrev();
            }
            if (newKey.IsKeyDown(Keys.Right) && oldKey.IsKeyUp(Keys.Right))
            {
                menu.SelectedItem.ShiftNext();
            }
            if (newKey.IsKeyDown(Keys.Down) && oldKey.IsKeyUp(Keys.Down))
            {
                menu.ShiftPrev();
            }
            if (newKey.IsKeyDown(Keys.Up) && oldKey.IsKeyUp(Keys.Up))
            {
                menu.ShiftNext();
            }
            if (newKey.IsKeyDown(Keys.Enter) && oldKey.IsKeyUp(Keys.Enter))
            {
                menu.SelectedItem.Hit();
                menu.FadeOut(500, EaseFunction.InSine, e => this.Close());
            }
            base.Update(gameTime);
        }
    }
}
